The Essence of JavaScript
نویسندگان
چکیده
We reduce JavaScript to a core calculus structured as a small-step operational semantics. We present several peculiarities of the language and show that our calculus models them. We explicate the desugaring process that turns JavaScript programs into ones in the core. We demonstrate faithfulness to JavaScript using real-world test suites. Finally, we illustrate utility by defining a security property, implementing it as a type system on the core, and extending it to the full language. 1 The Need for Another JavaScript Semantics The growing use of JavaScript has created whole new technical and business models of program construction and deployment. JavaScript is a feature-rich language with many quirks, and these quirks are often exploited by security and privacy attacks. This is especially true in cases where JavaScript has a familiar syntax but an unconventional semantics. Due to its popularity and shortcomings, companies and researchers have tried to tame JavaScript via program analyses [4, 9, 10, 13], sub-language [5, 7, 17], and more. These works claim but do not demonstrate soundness, partly because we lack a tractable account of the language. The JavaScript standard [6] is capacious and informal, while one major formal semantics [15] is large, not amenable to conventional proof techniques, and inherits the standard’s complexities, as we discuss in section 5. In contrast: – We present a core language, λJS , that embodies JavaScript’s essential features (sans eval). λJS fits on three pages and lends itself well to proof techniques such as subject reduction. – We show that we can desugar JavaScript into λJS . In particular, desugaring handles notorious JavaScript features such as this and with, so λJS itself remains simple (and thus simplifies proofs that utilize it). – We mechanize both λJS and desugaring. – To show compliance with reality, we successfully test λJS and desugaring against the actual Mozilla JavaScript test suite. – Finally, we demonstrate the use of our semantics by building a safe subset of JavaScript. This application highlights how our partitioning of JavaScript into core and syntactic sugar lends structure to proofs. Our supplemental materials (full desugaring, tools, etc.) are available at http://www.cs.brown.edu/research/plt/dl/jssem/v1/ c = num | str | bool | undefined | null v = c | func(x · · ·) { return e } | { str:v· · · } e = x | v | let (x = e) e | e(e · · ·) | e[e] | e[e] = e | delete e[e] E = • | let (x = E) e | E(e · · ·) | v(v · · · E, e · · ·) | {str: v · · · str:E, str:e · · · } | E[e] | v[E] | E[e] = e | v[E] = e | v[v] = E | delete E[e] | delete v[E] let (x = v) e →֒ e[x/v] (E-Let) (func(x1 · · ·xn) { return e })(v1 · · · vn) →֒ e[x1/v1 · · ·xn/vn] (E-App) { · · · str: v · · · }[str] →֒v (E-GetField) strx 6∈ (str1 · · · strn) { str1: v1 · · · strn: vn } [strx] →֒ undefined (E-GetField-NotFound) { str1: v1 · · · stri: vi · · · strn: vn } [stri] = v →֒ { str1: v1 · · · stri: v · · · strn: vn } (E-UpdateField) strx 6∈ (str1 · · ·) { str1: v1 · · · } [strx] = vx →֒ { strx: vx, str1: v1 · · · } (E-CreateField) delete { str1: v1 · · · strx: vx · · · strn: vn } [strx] →֒ { str1: v1 · · · strn: vn } (E-DeleteField) strx 6∈ (str1 · · ·) delete { str1: v1 · · · } [strx] →֒ { str1: v1 · · · } (E-DeleteField-NotFound) Fig. 1. Functions and Objects 2 λJS: A Tractable Semantics for JavaScript JavaScript is full of surprises. Syntax that may have a conventional interpretation for many readers often has a subtly different semantics in JavaScript. To aid the reader, we introduce λJS incrementally. We include examples of JavaScript’s quirks and show how λJS faithfully models them. Figures 1, 2, 4, 8, and 9 specify the syntax and semantics of λJS . We use a Felleisen-Hieb small-step operational semantics with evaluation contexts [8]. We typeset λJS code in a sans-serif typeface, and JavaScript in a fixed-width typeface.
منابع مشابه
Understanding TypeScript
TypeScript is an extension of JavaScript intended to enable easier development of large-scale JavaScript applications. While every JavaScript program is a TypeScript program, TypeScript offers a module system, classes, interfaces, and a rich gradual type system. The intention is that TypeScript provides a smooth transition for JavaScript programmers—well-established JavaScript programming idiom...
متن کاملFibrinogen ð-chain IgG ð-chain IgA ð-chain Antithrombin III Albumin Fibrinogen ext. Binary options
the immune response subsides. Figure 1. Eds, 1984. Optio ns Rituale Romanum. Much effort is been invested in search of (1) different performance of classical enzymes, Binary options journal khabar new biocatalysts, and (3) synthetic molecules exhibiting the essence of the biocatalytic activity.
متن کاملA Understanding JavaScript Event-Based Interactions with Clematis
Web applications have become one of the fastest growing types of software systems today. Despite their popularity, understanding the behaviour of modern web applications is still a challenging endeavour for developers during development and maintenance tasks. The challenges mainly stem from the dynamic, eventdriven, and asynchronous nature of the JavaScript language. We propose a generic techni...
متن کاملGULFSTREAM: Staged Static Analysis for Streaming JavaScript Applications
The advent of Web 2.0 has led to the proliferation of client-side code that is typically written in JavaScript. Recently, there has been an upsurge of interest in static analysis of client-side JavaScript for applications such as bug finding and optimization. However, most approaches in static analysis literature assume that the entire program is available to analysis. This, however, is in dire...
متن کاملFluid Object Types
Objects in popular scripting languages are lightweight and dynamic. Programmers use these objects in ways that confound existing type systems. We present a core calculus, λ S , that distills the essence of objects in a number of scripting languages. Using λ S , we examine characteristic object-oriented scripting idioms that are untypable by conventional type systems. We develop fluid object typ...
متن کاملGulfstream: Incremental Static Analysis for Streaming JavaScript Applications
The advent of Web 2.0 has led to the proliferation of client-side code that is typically written in JavaScript. Recently, there has been an upsurge of interest in static analysis of client-side JavaScript. However, most approaches in static analysis literature assume that the entire program is available to analysis. This, however, is in direct contradiction with the nature of Web 2.0 programs t...
متن کامل